dojo.declare("DMCustomerOrderSelectPgLatest", wm.Page, {

    totalOrderAmount: 0,
    start: function() {
        this.txtSearch.clear();
        this.largeTextArea1.clear();

    },
    "preferredDevice": "phone",

    isNumber: function(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    },



    list1ItemIdFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRowTitle'>Item Name: " + rowObj.desc + "</div>" + "<div class='MobileRow'>Quantity: " + rowObj.quantity + "</div>" + "<div class='MobileRow'>OrderedPrice: " + rowObj.orderedPrice + "</div>";


        return display;
    },



    listFinalOrderItemNameFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRowTitle'>" + rowObj.quantity + " " + rowObj.itemName + " <br>  Price: " + (rowObj.orderedPrice * rowObj.quantity) + " Rs </div>";
        //var display = "<div class='MobileRowTitle'>"+ rowObj.quantity +" " + rowObj.itemName + "</div>" + "<div class='MobileRow'>Quantity: " + rowObj.quantity + "  Price: " + rowObj.orderedPrice + " Rs </div>";
        return display;
    },

    listVendorItemsPriceFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRow' style='font-size:13px'><font color='red'>Price </font></div>  <div class='MobileRow' style='font-size:13px'><b> <font color='red'>" + rowObj.price + " Rs</font></b></div>";

        return display;

    },
    lvVendorItemsSuccess: function(inSender, inDeprecated) {

        this.renderVendorItemList();


    },



    selectOrder: function(inSender) {
        // console.error(inSender);
    },

    showDescription: function(inSender) {


        var imgId = inSender.id;
        var rowId = imgId.substr(imgId.indexOf('_') + 1);
        var itemObj = this.lvVendorItems.getItem(rowId);
        //        this.ddItemDesc.setShowing(true, false, true);
        this.ddItemDesc.setShowing(true);
        // this.labelItemName.setCaption(itemObj.data.itemName);
        this.labelDescription.setCaption(itemObj.data.itemDesc);



    },
    showItemDescription: function(rowId) {
        //var itemObj = this.lvVendorItems.getItem(rowId);
        //        this.ddItemDesc.setShowing(true, false, true);
        this.ddItemDesc.setShowing(true);
        // this.labelItemName.setCaption(itemObj.data.itemName);
        this.labelDescription.setCaption(rowId);
    },


    bOrderPreviewClick2: function(inSender) {
        //method for new check out
        wm.Page.getPage("MCustomerHomePg").vPageBack = "CHECK_OUT";
        this.largeTextArea1.clear();
        
        //hide the search
        this.vOrderedItems.clearData();
        var totalPrice = 0;
        this.totalOrderAmount = 0;
        for (var count = 0; count < this.lvVendorItems.getCount(); count++) {

            var vItem = this.lvVendorItems.getItem(count);
            if (vItem.data.tax > 0) {

                var qty = vItem.data.tax;
                var price = (qty * vItem.data.price);
                price = Math.round(price * 100) / 100;
                totalPrice = totalPrice + price;
                totalPrice = Math.round(totalPrice * 100) / 100;
                this.vOrderedItems.addItem({
                    itemId: vItem.data.itemId,
                    itemName: vItem.data.itemName,
                    quantity: vItem.data.tax,
                    orderedPrice: vItem.data.price,
                    desc: vItem.data.itemDesc
                });
            }
        }


        if (this.vOrderedItems.getCount() > 0) {
            this.totalOrderAmount = totalPrice;
            this.layerPlaceOrder.activate();
            this.panel2.setShowing(true);
            this.labelTotalOrderPrice.setShowing(true);
           // this.labelSelectItem.setShowing(false);
            this.labelTotalOrderPrice.setCaption("Total:" + totalPrice + " Rs");
            this.buttonConfirmOrder.setShowing(true);
           // this.labelReviewOrder.setShowing(true);
            this.panel9.setShowing(false);
           

        } else {
            app.toastWarning("Please Select atleast one Item", 2000);

           // this.labelReviewOrder.setShowing(false);
           // this.labelSelectItem.setShowing(true);
            this.labelTotalOrderPrice.setShowing(false);
            this.buttonConfirmOrder.setShowing(false);


        }


    },
    buttonAnotherOrderClick: function(inSender) {

        wm.Page.getPage("MCustomerHomePg").ncServices.update();

    },

    buttonConfirmOrderClick: function(inSender) {
         wm.Page.getPage("MCustomerHomePg").vPageBack = "CONFIRM_ORDER";
        var mainPage = wm.Page.getPage("MobileMainPg");
        var custMainPage = wm.Page.getPage("MCustomerHomePg");
        var orderBlock = false;
        this.panel9.setShowing(false);

        if (custMainPage.vVendorMinAmount) {


            if (custMainPage.vVendorMinAmount && custMainPage.vVendorMinAmount > this.totalOrderAmount) {
                orderBlock = true;
            }
        }

        if (!orderBlock) {
if(this.totalOrderAmount>0)
{
            this.svCompleteOrder.input.setValue("order.customerId", mainPage.vUserId);

            this.svCompleteOrder.update();
            this.layerOrderResult.activate();
}else{
     app.toastWarning("Please select atleast one item", 2000);
}
        } else {
            app.toastPosition = "center center";
            app.toastError("Minimum Order Amount is Rs" + custMainPage.vVendorMinAmount, 2000);

        }

    },
    svCompleteOrderSuccess1: function(inSender, inDeprecated) {

        this.vOrderedItems.clearData();



    },
    mbBackServicesClick: function(inSender) {
        wm.Page.getPage("MCustomerHomePg").ncServices.update();
    },
    svCompleteOrderError1: function(inSender, inError) {
        this.vOrderedItems.clearData();
    },
    listItemsItemNameFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {


    },

    listItemsGridAddCartClick: function(rowIndex) {


        for (var count = 0; count < this.lvVendorItems.getCount(); count++) {

            var vItem = this.lvVendorItems.getItem(count);
            if (vItem.data.itemId === rowIndex) {
                if (vItem.data.tax) {
                    vItem.data.tax = vItem.data.tax + 1;
                } else {
                    vItem.data.tax = 1;
                }

                this.lvVendorItems.setItem(count, vItem);
            }
        }



    },
    listItemsGridRemoveCartClick: function(rowIndex) {


        for (var count = 0; count < this.lvVendorItems.getCount(); count++) {

            var vItem = this.lvVendorItems.getItem(count);
            if (vItem.data.itemId === rowIndex) {
                if (vItem.data.tax) {
                    vItem.data.tax = vItem.data.tax - 1;
                }

                this.lvVendorItems.setItem(count, vItem);
            }
        }
    },





    listItemsImageUrlFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var imgClaimHTML = '<br><br> No Image <br>';
        var unitVal = rowObj.unit;
        if (unitVal === null) {
            unitVal = "";
        }
        if (inValue !== null && inValue !== "") {
            var path = this.getRuntimeId() + ".showItemDescription('" + rowObj.itemDesc + "')";
            // alert(path);
            imgClaimHTML = '<img src="' + inValue + '" onclick="' + path + '" align="center" height="70px" width="70px")> ';
        }
        imgClaimHTML = imgClaimHTML + " <br>  <br>" + rowObj.itemName + " <br> " + rowObj.price + " " + rowObj.currency + " / " + unitVal;
        if (rowObj.quantity && rowObj.quantity > 0) {
            //imgClaimHTML= imgClaimHTML+" <br> <br> "+rowObj.quantity +" in Cart <br> ";
        } else {

        }

        return imgClaimHTML;
    },
    listItemsQuantityFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {


        var editTx = '<button  type="button"   onclick="' + this.getRuntimeId() + '.listItemsGridAddCartClick(' + rowObj.itemId + ')" height="40px" width="50px" > Add</button>';

        if (rowObj.tax && rowObj.tax > 0) {
            editTx = editTx + " <br> " + rowObj.tax + " in Cart <br> ";
        } else {
            editTx = editTx + "<br>";
        }
        editTx = editTx + '<button  type="button"   onclick="' + this.getRuntimeId() + '.listItemsGridRemoveCartClick(' + rowObj.itemId + ')" height="40px" width="50px" > Remove</button>';
        return editTx;


    },
    listCategoriesGridButtonClick: function(inSender, fieldName, rowData, rowIndex) {
        this.listItemsGridButtonClick(inSender, fieldName, rowData, rowIndex);
    },
    listCategoriesBrandFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var buttonText = '<img src ="resources/images/mobile/next_img.png" height="26px" />';
        return buttonText;
    },
    listCategoriesSelect: function(inSender, inItem) {

        var itemType = this.listCategories.selectedItem.data.itemType;
        wm.Page.getPage("MCustomerHomePg").vPageBack = "VENDOR_ITEM_CATEGORY";
        if (itemType !== null) {
            this.listItems.setQuery({
                itemType: itemType
            });
            this.panelVendorItems2.setShowing(false);
        }


    },
    bSearchClick: function(inSender) {
        var itemName = this.txtSearch.dataValue;

        wm.Page.getPage("MCustomerHomePg").vPageBack = "VENDOR_ITEM_CATEGORY";
        if (itemName && itemName !== null && itemName !== "") {
            this.listItems.setQuery({
                itemName: "*" + itemName + "*"
            });
            var newList = this.lvVendorItems.query({
                itemName: "*" + itemName + "*"
            });
            if (newList.getCount() < 1) {
                this.panelVendorItems2.setShowing(true);
            } else {

                this.panelVendorItems2.setShowing(false);
            }
        } else {
            this.panelVendorItems2.setShowing(false);
            this.listItems.setQuery({});
        }
    },
    bCancelOrderClick1: function(inSender) {
        this.panel9.setShowing(true);
    },
    layerSelectCategoryShow: function(inSender) {
        this.panel9.setShowing(true);
    },
    layerSelectOrderShow: function(inSender) {
        this.panel9.setShowing(true);
    },
    refreshVendorItemList: function() {

        this.vOrderedItems.clearData();

        try {

            for (var count = 0; count <= this.lvVendorItems.getCount(); count++) {

                var vItem = this.lvVendorItems.getItem(count);

                if (vItem && vItem.data.tax) {
                    vItem.data.tax = 0;
                    this.lvVendorItems.setItem(count, vItem);
                }
            }


        } catch (e) {

            console.log(e);
        }
        this.panel9.setShowing(true);
    },
    listItemsGridButtonClick: function(inSender, fieldName, rowData, rowIndex) {

        for (var count = 0; count < this.lvVendorItems.getCount(); count++) {

            var vItem = this.lvVendorItems.getItem(count);
            if (vItem.data.itemId === rowData.itemId) {
                if (fieldName === "price") {

                    if (vItem.data.tax) {
                        vItem.data.tax = vItem.data.tax - 1;
                        this.ncOrderUpdateRemove.update();

                    }
                }
                if (fieldName === "tax") {
                    if (vItem.data.tax) {
                        vItem.data.tax = vItem.data.tax + 1;
                    } else {
                        vItem.data.tax = 1;
                    }
                    this.ncOrderUpdate.update();
                }


                this.lvVendorItems.setItem(count, vItem);

            }
        }


    },
    buttonBackClick2: function(inSender) {
        this.txtSearch.clear();
    },
    listCategoriesSelect2: function(inSender, inItem) {
        this.txtSearch.clear();
    },
    listFinalOrder1GridButtonClick: function(inSender, fieldName, rowData, rowIndex) {
        this.totalOrderAmount = 0;
        for (var count = 0; count < this.vOrderedItems.getCount(); count++) {

            var vItem = this.vOrderedItems.getItem(count);
            if (vItem.data.itemId === rowData.itemId) {

                if (fieldName === "quantity") {
                    if (vItem.data.quantity) {
                        vItem.data.quantity = vItem.data.quantity + 1;
                        this.ncOrderUpdate.update();
                    } else {
                        vItem.data.quantity = 1;
                        this.ncOrderUpdate.update();
                        
                        }
                    
                }
                //remove from cart
                if (fieldName === "orderedPrice") {

                    if (vItem.data.quantity) {
                        vItem.data.quantity = vItem.data.quantity - 1;

                        this.ncOrderUpdateRemove.update();

                    }
                   // this.vOrderedItems.removeItem(count);
                }
               this.vOrderedItems.setItem(count, vItem);



            }
            var price = vItem.data.orderedPrice * vItem.data.quantity;
            price = Math.round(price * 100) / 100;
            this.totalOrderAmount = this.totalOrderAmount + price;

        }
        this.labelTotalOrderPrice.setCaption("Total:" + this.totalOrderAmount + " Rs");
    },
    listFinalOrder1QuantityFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {

        var editTx = '<button  type="button"   onclick="' + this.getRuntimeId() + '.listUpdateConfirmOrderAdd(' + rowObj.itemId + ')" height="40px" width="50px" > Add</button>';

        if (rowObj.quantity && rowObj.quantity > 0) {
            editTx = editTx + " <br> " + rowObj.quantity + " in Cart <br> ";
        } else {
            editTx = editTx + "<br>";
        }
        editTx = editTx + '<button  type="button"   onclick="' + this.getRuntimeId() + '.listUpdateConfirmOrderRemove(' + rowObj.itemId + ')" height="40px" width="50px" > Remove</button>';
        return editTx;

    },
    listUpdateConfirmOrderAdd: function(rowIndex) {
        this.totalOrderAmount = 0;

        for (var count = 0; count < this.vOrderedItems.getCount(); count++) {

            var vItem = this.vOrderedItems.getItem(count);
            var price = vItem.data.orderedPrice * vItem.data.quantity;
            price = Math.round(price * 100) / 100;
            if (vItem.data.itemId === rowIndex) {
                if (vItem.data.quantity) {
                    vItem.data.quantity = vItem.data.quantity + 1;
                    price = vItem.data.orderedPrice * vItem.data.quantity;
                    this.ncOrderUpdate.update();
                }
                this.vOrderedItems.setItem(count, vItem);

            }
            this.totalOrderAmount = this.totalOrderAmount + price;

        }
        this.labelTotalOrderPrice.setCaption("Total:" + this.totalOrderAmount + " Rs");
    },
    listUpdateConfirmOrderRemove: function(rowIndex) {

        this.totalOrderAmount = 0;

        for (var count = 0; count < this.vOrderedItems.getCount(); count++) {

            var vItem = this.vOrderedItems.getItem(count);
            var price = vItem.data.orderedPrice * vItem.data.quantity;
            price = Math.round(price * 100) / 100;
            if (vItem.data.itemId === rowIndex) {
                if (vItem.data.quantity) {
                    vItem.data.quantity = vItem.data.quantity - 1;
                    price = vItem.data.orderedPrice * vItem.data.quantity;
                    this.ncOrderUpdateRemove.update();
                }
                this.vOrderedItems.setItem(count, vItem);

            }
            this.totalOrderAmount = this.totalOrderAmount + price;
            this.labelTotalOrderPrice.setCaption("Total:" + this.totalOrderAmount + " Rs");
        }
    },
    listFinalOrder1OrderedPriceFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {

    },
    layerPlaceOrderShow: function(inSender) {
		this.largeTextArea1.clear();
	},
	_end: 0
});